package GUILayer;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;

import CtrLayer.CustomerCtr;
import CtrLayer.CustomerCtr.InvalidZipcodeException;

import java.awt.Dialog.ModalityType;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AddCustomer extends JDialog
{

	private final JPanel contentPanel = new JPanel();
	private JTextField txtName;
	private JTextField txtAddress;
	private JTextField txtZipcode;
	private JTextField txtPhone;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args)
	{
		try
		{
			AddCustomer dialog = new AddCustomer();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public AddCustomer()
	{
		setModalityType(ModalityType.APPLICATION_MODAL);
		setTitle("Add Customer");
		setBounds(100, 100, 254, 191);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblName = new JLabel("Name:");
			lblName.setBounds(10, 11, 88, 14);
			contentPanel.add(lblName);
		}
		{
			txtName = new JTextField();
			txtName.setBounds(108, 8, 86, 20);
			contentPanel.add(txtName);
			txtName.setColumns(10);
		}
		{
			JLabel lblAddress = new JLabel("Address:");
			lblAddress.setBounds(10, 39, 88, 14);
			contentPanel.add(lblAddress);
		}
		{
			txtAddress = new JTextField();
			txtAddress.setColumns(10);
			txtAddress.setBounds(108, 36, 86, 20);
			contentPanel.add(txtAddress);
		}
		{
			JLabel lblZipcode = new JLabel("Zipcode:");
			lblZipcode.setBounds(10, 67, 88, 14);
			contentPanel.add(lblZipcode);
		}
		{
			txtZipcode = new JTextField();
			txtZipcode.setColumns(10);
			txtZipcode.setBounds(108, 64, 86, 20);
			contentPanel.add(txtZipcode);
		}
		{
			JLabel lblPhoneNumber = new JLabel("Phone number:");
			lblPhoneNumber.setBounds(10, 94, 88, 14);
			contentPanel.add(lblPhoneNumber);
		}
		{
			txtPhone = new JTextField();
			txtPhone.setColumns(10);
			txtPhone.setBounds(108, 91, 86, 20);
			contentPanel.add(txtPhone);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent arg0)
					{
						// Lav controller.
						CustomerCtr c = new CustomerCtr();
						// Konverter streng til int.
						int zipcode;
						try
						{
							zipcode = Integer.parseInt(txtZipcode.getText());
							c.addCustomer(txtName.getText(), txtAddress.getText(), zipcode, txtPhone.getText());
						}
						catch (NumberFormatException e)
						{
							JOptionPane.showMessageDialog(null,
								    "Zipcode must be a number!",
								    "Invalid format",
								    JOptionPane.WARNING_MESSAGE);
							return;
						}
						catch (InvalidZipcodeException e)
						{
							JOptionPane.showMessageDialog(null,
								    "Invalid zipcode!",
								    "Invalid zipcode",
								    JOptionPane.WARNING_MESSAGE);
							return;
						}

						
						setVisible(false);
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}

			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent arg0)
					{
						setVisible(false);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

}
